﻿'use strict';

define([
    'backbone',
], function (Backbone) {
    var Model = Backbone.Model.extend({
        superClass: null,

        defaults: {
            billId: null,
            //id: 0,
            name: '',
            code: '',
            pricePerUnit: 0,
            //priceTotal: 0,
            productId: null,
            count: 0,
        },

        constructor: function () {
            this.superClass = Model.__super__;

            this.superClass.constructor.apply(this, arguments);
        },
        initialize: function () {
            this.listenTo(this, 'change:count change:pricePerUnit', this._change);
        },

        get: function (attr) {
            var retVal;

            if (attr) {
                var attrFixed = $.trim(attr).toUpperCase();

                switch (attrFixed) {
                    case "PRICETOTAL":
                        retVal = this.get('count') * this.get('pricePerUnit');
                        break;
                    default:
                        retVal = this.superClass.get.call(this, attr);
                        break;
                }
            }
            else {
                retVal = this.superClass.get.call(this, attr);
            }

            return retVal;
        },
        toJSON: function () {
            return _.extend(
                this.superClass.toJSON.call(this),
                {
                    priceTotal: this.get('priceTotal'),
                }
            );
        },


        _change: function (model, options) {
            this.trigger('change:priceTotal');
        }
    });

    return Model;
});